package zip;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyZip extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private static public File opened_file, other_file;
    static long past, future;
    static JLabel redLabel, blueLabel, redScore, blueScore;
    static JPanel buttonPanel, titlePanel, scorePanel;
    static JButton huf, unhuf, exit;

    public JPanel createContentPane() {
        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);

        titlePanel = new JPanel();
        titlePanel.setLayout(null);
        titlePanel.setLocation(90, 20);
        titlePanel.setSize(170, 70);
        totalGUI.add(titlePanel);

        redLabel = new JLabel("初始文件大小: ");
        redLabel.setLocation(43, 0);
        redLabel.setSize(150, 30);
        redLabel.setHorizontalAlignment(0);
        titlePanel.add(redLabel);

        blueLabel = new JLabel("压缩/解压后大小: ");
        blueLabel.setLocation(10, 30);
        blueLabel.setSize(170, 30);
        blueLabel.setHorizontalAlignment(0);
        titlePanel.add(blueLabel);

        scorePanel = new JPanel();
        scorePanel.setLayout(null);
        scorePanel.setLocation(270, 20);
        scorePanel.setSize(320, 60);
        totalGUI.add(scorePanel);

        redScore = new JLabel("");
        redScore.setLocation(0, 0);
        redScore.setSize(300, 30);
        redScore.setHorizontalAlignment(0);
        scorePanel.add(redScore);

        blueScore = new JLabel("");
        blueScore.setLocation(0, 30);
        blueScore.setSize(300, 30);
        blueScore.setHorizontalAlignment(0);
        scorePanel.add(blueScore);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(null);
        buttonPanel.setLocation(10, 130);
        buttonPanel.setSize(5200, 150);
        totalGUI.add(buttonPanel);

        huf = new JButton("压缩");
        huf.setLocation(100, 0);
        huf.setSize(120, 30);
        huf.addActionListener(this);
        buttonPanel.add(huf);

        unhuf = new JButton("解压缩");
        unhuf.setLocation(290, 0);
        unhuf.setSize(120, 30);
        unhuf.addActionListener(this);
        buttonPanel.add(unhuf);

        exit = new JButton("EXIT");
        exit.setLocation(130, 70);
        exit.setSize(250, 30);
        exit.addActionListener(this);
        buttonPanel.add(exit);

        totalGUI.setOpaque(true);
        return totalGUI;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == exit) {
            System.exit(0);
        } else {
            if (opened_file == null || opened_file.getPath().length() == 0) {
                JOptionPane.showMessageDialog(null, "请从 File 选项中选择一个文件", "Status", JOptionPane.PLAIN_MESSAGE);
                return;
            }
            if (e.getSource() == huf) {

                HuffZip.doHuffZip(opened_file.getPath());
                JOptionPane.showMessageDialog(null,
                        "..........................Zipping Finished..........................", "Status",
                        JOptionPane.PLAIN_MESSAGE);
                redScore.setText(opened_file.length() + "Bytes (" + opened_file.getName() + ")");
                other_file = new File(opened_file.getPath() + ".huff");
                future = other_file.length();
                blueScore.setText(future + "Bytes (" + other_file.getName() + ")");
            } else if (e.getSource() == unhuf) {
                String path = opened_file.getPath();
                if (!path.endsWith(".huff")) {
                    JOptionPane.showMessageDialog(null, "要解压的文件后缀名不正确哟~\n请重新在 File 处选择文件", "Status", JOptionPane.PLAIN_MESSAGE);
                    return;
                }
                HuffUnzip.doUnzip(opened_file.getPath());
                JOptionPane.showMessageDialog(null,
                        "..........................UnZipping Finished..........................", "Status",
                        JOptionPane.PLAIN_MESSAGE);
                redScore.setText(opened_file.length() + "Bytes (" + opened_file.getName() + ")");
                String s = opened_file.getPath();
                s = s.substring(0, s.length() - ".huff".length());
                other_file = new File(s);
                future = other_file.length();
                blueScore.setText(future + "Bytes (" + other_file.getName() + ")");
            }
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Huffman Zip by Bankarian");

        // Create and set up the content pane.
        MyZip demo = new MyZip();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(350, 170, 550, 300);

        JMenu fileMenu = new JMenu("File");

        JMenuBar bar = new JMenuBar();
        frame.setJMenuBar(bar);
        bar.add(fileMenu);

        JMenuItem openItem = new JMenuItem("Open");
        fileMenu.add(openItem);
        openItem.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.showDialog(new JLabel(), "选择文件");
            opened_file = fileChooser.getSelectedFile();
            past = opened_file.length();
            redScore.setText(past + "Bytes (" + opened_file.getName() + ")");
            blueScore.setText("NotYetCalculated");
        });

        JMenuItem exitItem = new JMenuItem("Exit");
        fileMenu.add(exitItem);
        exitItem.addActionListener(event -> System.exit(0));

        JMenu helpMenu = new JMenu("Help");
        frame.setJMenuBar(bar);
        bar.add(helpMenu);

        JMenuItem helpItem = new JMenuItem("How To");
        helpMenu.add(helpItem);
        helpItem.addActionListener(event -> JOptionPane.showMessageDialog(null,
                "基于哈夫曼编码的算法实现" + "\n" + "在 File 选项中找到要压缩或者要解压的文件" + "\n" + "压缩完成的文件会多出 .huff 后缀名" + "\n"
                        + "只支持解压 .huff 后缀的压缩文件，解压后的文件 .huff 后缀消失" + "\n",
                "How To...", JOptionPane.PLAIN_MESSAGE));
        JMenuItem aboutItem = new JMenuItem("About");
        helpMenu.add(aboutItem);

        aboutItem.addActionListener(event -> JOptionPane.showMessageDialog(null, "此软件利用Java开发，作者Bankarian" + "\n", "About",
                JOptionPane.PLAIN_MESSAGE));
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        SwingUtilities.invokeLater(MyZip::createAndShowGUI);
    }

}